package com.hualife.sdk.controller;


import com.alibaba.fastjson.JSONObject;
import com.hualife.sdk.model.RequestVO;
import com.hualife.sdk.utils.MyHttpClient;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;


@RestController()
public class SdkController {

    //获取access_token的请求路径
    private static final String GETACCESSTOKEN = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET";
    private static final String QYWXURL = "https://qyapi.weixin.qq.com/cgi-bin/";

    @Autowired
    private RestTemplate restTemplate;

    @ApiOperation(value = "企业微信的POST请求")
    @PostMapping("/sever")
    public String sever(@RequestBody RequestVO requestVO){
        System.out.println("=====================START=====================");
        if (requestVO.getMethod().equals("GET")){
            return sendGET(requestVO);
        }else {
            return sendPOST(requestVO);
        }
    }

    @ApiOperation(value = "企业微信的前端请求")
    @PostMapping("/web")
    public String web(@RequestBody RequestVO requestVO){
        System.out.println("=====================START=====================");
        if (requestVO.getMethod().equals("GET")){
            ResponseEntity<String> res = restTemplate.getForEntity(requestVO.getUrl(), String.class);
            String body = res.getBody();
            System.out.println(body);
            return body;
        }else {
            //创建请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            JSONObject jsonObject = JSONObject.parseObject(requestVO.getParameters());
            HttpEntity<JSONObject> entity = new HttpEntity<JSONObject>(jsonObject, headers);
            ResponseEntity<String> res = restTemplate.postForEntity(requestVO.getUrl(), entity, String.class);
            String body = res.getBody();
            System.out.println(body);
            return body;
        }
    }

    @ApiOperation("获取图片并返回")
    @GetMapping("/getKey")
    public String getKey() throws IOException {
        String myGet = MyHttpClient.myGet();
        String substringBetween = StringUtils.substringBetween(myGet, "\"key\":\"", "\"");
        return substringBetween;
    }

    @ApiOperation("通过key获取图片并返回")
    @GetMapping("/getImg")
    public ResponseEntity<byte[]> getImg(String key){

        String url = "https://open.work.weixin.qq.com/wwopen/sso/qrImg?key=";

        /**
         * 请求地址
         * 请求方式
         * 请求实体类
         * 请求返回类型
         */
        ResponseEntity<byte[]> responseEntity = restTemplate.exchange(url+key, HttpMethod.GET, null, byte[].class);
        //获取entity中的数据
        byte[] body = responseEntity.getBody();
        //将输入流转为byte[]
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        return new ResponseEntity<>(body,headers, HttpStatus.OK);
    }

    @ApiOperation("获取code")
    @GetMapping("/getCode")
    public String getCode(String key) throws IOException {
        //String url = "https://open.work.weixin.qq.com/wwopen/sso/l/qrConnect?callback=jsonpCallback&key=KEY&redirect_uri=http%3A%2F%2Fsales-int.ihxlife.com&appid=ww60f69edf84f8d24f&_=1639644654952";
        String url = "https://open.work.weixin.qq.com/wwopen/sso/l/qrConnect?callback=jsonpCallback&key=KEY&redirect_uri=http%3A%2F%2Fsales-int.ihxlife.com&appid=wx7dba28868b3795c5&_=1639644654952";

        url = url.replace("KEY", key);
        ResponseEntity<String> res = restTemplate.getForEntity(url, String.class);
        String body = res.getBody();

        return body;
    }



    public String sendGET(RequestVO requestVO){
        System.out.println("=====================GET-START=====================");
        System.out.println(requestVO);
        ResponseEntity<String> res = restTemplate.getForEntity(QYWXURL+requestVO.getUrl(), String.class);
        String body = res.getBody();
        System.out.println(body);
        System.out.println("=====================GET-END=====================");
        return body;
    }
    public String sendPOST(RequestVO requestVO){
        System.out.println("=====================POST-START=====================");
        System.out.println(requestVO);
        //创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonObject = JSONObject.parseObject(requestVO.getParameters());
        HttpEntity<JSONObject> entity = new HttpEntity<JSONObject>(jsonObject, headers);

        ResponseEntity<String> res = restTemplate.postForEntity(QYWXURL+requestVO.getUrl(), entity, String.class);
        String body = res.getBody();
        System.out.println(body);
        System.out.println("=============通过 堡垒机地址访问  https://10.0.3.110/index.php?locale=zh_CN.UTF-8\n通过 堡垒机地址访问  https://10.0.3.110/index.php?locale=zh_CN.UTF-8\n通过 堡垒机地址访问  https://10.0.3.110/index.php?locale=zh_CN.UTF-8\n通过 堡垒机地址访问  https://10.0.3.110/index.php?locale=zh_CN.UTF-8\n通过 堡垒机地址访问  https://10.0.3.110/index.php?locale=zh_CN.UTF-8\n通过 堡垒机地址访问  https://10.0.3.110/index.php?locale=zh_CN.UTF-8\n========POST-END=====================");
        return body;
    }
//    @ApiOperation("获取图片并返回")
//    @GetMapping("/getHtml")
//    public String getHtml() throws IOException {
//        String myGet = MyHttpClient.myGet();
//
//        return myGet;
//    }

}
